import Api,{FEEDBACK} from '../../../utils/api.js';
import { ossupload } from '../../../utils/ossupload.js';
import { handPic, error } from '../../../utils/util.js';


Page({
    data: {
        comment: [],     // 评价内容
        imgs: [],        // 评价图片列表
        showimgs: [],

        disable: true,
        // upload_show: false,
        question_type: [
            {val:1,name: "功能异常", description: "不能正常使用现有功能", checked: false },
            {val:2, name: "使用建议", description: "用的不满意的地方都踢过来吧", checked: false },
            {val:3, name: "功能需求", description: "现有功能不能满足", checked: false },
        ],
        form: {
            type: "",
            description: "",
            tel: ''
        }
    },
    // 选择问题类型   
    choose_question: function (e) {
        let disable = true;
        const { question_type, form } = this.data;
        const type = e.target.dataset.type;
        question_type.map((item, idx) => {
            item.checked = false;
        });

        question_type[type].checked = true;
        form.type = question_type[type].val;
        form.description != "" ? disable = false : disable = true
        this.setData({ question_type: question_type, form: form, disable: disable })
    },

    // ======== 保存文本域和input的vuale值 =====
    save_Value: function (e) {
        const { form } = this.data;
        const type = e.target.dataset.type;

        if (type == 'textarea') {
            let disable = true;
            form.description = e.detail.value.replace(/(^\s*)|(\s*$)/g, "");
            if (form.description != "" && form.type != "") {
                disable = false
            }
            this.setData({ form: form, disable: disable })
        } else if (type == "input") {
            form.tel = e.detail.value;
            this.setData({ form: form })
        }
    },
    /** 图片上传 */
    upimg: function () {
        const that = this;
        const { imgs, showimgs } = this.data;

        ossupload(function (key) {
            let url = handPic(key, 4);
            imgs.push(key);
            showimgs.push(url);
            that.setData(
                {
                    imgs: imgs,
                    showimgs: showimgs
                }
            )
        });
    },
    /** 删除图片 */
    del_upimg: function (e) {
        let { idx } = e.target.dataset;
        let { imgs, showimgs } = this.data;
        const that = this;

        wx.showModal({
            content: '确认删除图片',
            success: function (res) {
                if (res.confirm) {
                    imgs.splice(idx, 1);
                    showimgs.splice(idx, 1);
                    that.setData({
                        imgs: imgs,
                        showimgs: showimgs
                    });
                } else if (res.cancel) {

                }
            }
        });
    },
    //  表单提交
    submit: function () {

        let _this = this;
        let {key, form, disable,imgs} = this.data;
        if (!disable) {
            if (form.tel != "") {
                const status = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(form.tel);
               
                if (!status) {
                    wx.showToast({
                        title: '手机号格式不正确',
                        duration: 2000
                    })
                }
            }

            if (imgs) {
                imgs = imgs.join(',');
            }
            

            Api.fetch(FEEDBACK,{
                key:key,
                content:form.description,
                type:form.type,
                imgs:imgs,
                mobile:form.tel
            },resp => {
            
                form.description = '';
                form.tel = '';

                _this.setData({
                    form:form,
                    disable:true,
                    imgs:[],
                    showimgs:[]
                })
                
                wx.showToast({
                    title: '已收到您的反馈!',
                })


                
            })

            // 提交form
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let _this = this,
            key;
        //判断是否登录
        if (Api.checkLogin(function () {
            _this.onLoad(options);
        })) {
            key = Api.getKey();
            _this.setData({ key: key })
        }
    },
    /** 页面上拉触底事件的处理函数 */
    onReachBottom: function () {

    },

    /**  用户点击右上角分享 */
    onShareAppMessage: function () {

    }
})